library Wave3 initializer init requires LegacySystem

    private function ArtillerySupport takes nothing returns nothing
        local location l = GetUnitLoc(udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]])
        local real x = GetLocationX(l)
        local real y = GetLocationY(l)
        local real r
        local real theta
        local real maxx = GetRectMaxX(bj_mapInitialPlayableArea)
        local real maxy = GetRectMaxY(bj_mapInitialPlayableArea)
        local real minx = GetRectMinX(bj_mapInitialPlayableArea)
        local real miny = GetRectMinY(bj_mapInitialPlayableArea)
        local real ux
        local real uy
        local unit u
        local real dx
        local real dy
        loop
            set r = GetRandomReal(1200,2000)
            set theta = GetRandomReal(0,6.28)
            set ux = Clamp(x+r*Cos(theta),minx,maxx)
            set uy = Clamp(y+r*Sin(theta),miny,maxy)
            set dx = ux + 970.0
            set dy = uy + 135.0
            exitwhen dx*dx+dy*dy>4000000 //2000 radius
        endloop
        if udg_CurrentWave!=3 or udg_Players<1 then
            call DisableTrigger(GetTriggeringTrigger())
            call GeneralText(3.0, "|cfff3ad00Battalion|r : We've got most of them." )
        endif

		
        call PingMinimapEx(ux, uy, 10.0, 204, 196, 115, false)
        set u = CreateUnit(Player(11), 'e005', ux, uy ,0.0 )
        call UnitApplyTimedLife(u,'Bhwd',10.5)
        call TriggerSleepAction(0.)
        set u = CreateUnit(Player(14),'n00P',ux,uy,0.0) //DangerZone Dummy
        call UnitApplyTimedLife(u,'Bhwd',10.5)
		
		
        call PolledWait(7.)
        set udg_soundpath = "war3mapImported\\FallingMissile.wav"
        set bj_meleeNearestMineDist = x
        set bj_lastTransmissionDuration = y
        call PlaySoundPath3D.execute()
        call PolledWait(3.)
        call SetNoise(4.0)

        call ArtHandler(udg_Residence41,ux,uy,10,800.0,450.0,2.0,1)

        call TriggerSleepAction(0.)
        call EarShockArea(ux,uy,800)
        if GetRandomInt(0,1)==1 then
            set udg_soundpath = "Sound\\Buildings\\Death\\BuildingDeathSmallHuman.wav"
        else
            set udg_soundpath = "Sound\\Buildings\\Death\\OrcBuildingDeathSmall1.wav"
        endif
        set bj_meleeNearestMineDist = x
        set bj_lastTransmissionDuration = y
        call PlaySoundPath3D.execute()
        call PolledWait(2.)
        call ClearNoise()
        call CameraSetTargetNoise(5.0,0.03)
        call PolledWait(3.)
		call RemoveLocation(l)
        set u = null
		set l  = null
    endfunction


    private function FemaleZombieEncounter takes nothing returns nothing
        local integer i
        if GetUnitTypeId(GetTriggerUnit())=='n00C' and IsUnitType(GetKillingUnit(),UNIT_TYPE_HERO)==true then
            call DestroyTrigger(GetTriggeringTrigger())
            set udg_soundpath = "Units\\Undead\\Banshee\\BansheeDeath.wav"
            call PlaySoundPath.execute()
            if GetRandomInt(1,3)==1 then
                call PolledWait(2.)
                set i = GetRandomInt(1, udg_Players)
                call FieldChat(udg_Hero[udg_Sorted[i]], 5.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : What the hell was that?" )
            endif
        endif
    endfunction

    private function Wave3Chatter takes nothing returns nothing
        call GeneralText(3.0, "|cfff3ad00Marine|r : Jesus H. Christ, there wasn't supposed to even BE any hostiles in this sector!" )
        call PolledWait(4.)
        call GeneralText(3.0, "|cfff3ad00Marine #2|r : Keep your finger on the trigger Marine." )
        call PolledWait(8.)
        call GeneralText(3.0, "|cfff3ad00Marine|r : There's too many of them! There's.. too many!" )
        call PolledWait(4.)
        call GeneralText(3.0, "|cfff3ad00Marine #2|r : You're gonna keep shooting until I say there's too god-damn many." )
    endfunction

    private function Wave3c takes nothing returns nothing
        local integer i = 1
        local integer ammodrop = 8
        local integer birdcount = 2
        set udg_CurrentWave = 3
        set udg_Visibility = 73
        call IntermissionRestore()
        set udg_Visibility = 72
        call IntermissionRestore()
        call GenericActions.execute()
        call SimpleChat(udg_Ogilvy, 5.0, "|cfff3ad00Ogilvy|r : Enough is enough. I've had it with these motherfuckin' zombies in this motherfuckin' town!" )
        call IssuePointOrder(udg_Ogilvy,"move",-1206.0,1222.0)
        call PolledWait(6.)
        if GetWidgetLife(udg_Ogilvy)>0.405 then
            call RemoveUnit(udg_Ogilvy)
            set udg_Ogilvy = CreateUnit(Player(9),'h00P',-1206.0,1222.0,0.0)
            call PumpShotgun(udg_Ogilvy)
        endif
		
		call TriggerSleepAction(0.1)
		if GetWidgetLife(udg_Ogilvy)>0.405 then
           call IssuePointOrderById(udg_Ogilvy,ORDER.patrol,-1000.0,-500.0)
        endif

        if udg_SquadXP>90000 then
            set ammodrop=6
            set birdcount=3
        endif
        call ItemSpawnN('shas',ammodrop) //Ammunition Box
        loop
            exitwhen i>5
            call RandomSpawn('ugar',birdcount,udg_Spawn)
            call PolledWait(7.)
            set i=i+1
        endloop
        if udg_Players>0 then
            if GetRandomInt(1,3)==1 then
                call ExecuteRegisteredFunction("Wave4b")
                //call Wave4b.execute()
            else
                call ExecuteRegisteredFunction("Wave4a")
                //call Wave4a.execute()
            endif
        endif
    endfunction

    private function Wave3b takes nothing returns nothing
        local integer i
        local integer i2=0
        local trigger t = CreateTrigger()
        local integer democrats = 0
        local group g = CreateGroup()
        local unit u
        local real x
        local real y
        set udg_CurrentWave = 3
        set udg_Visibility = 73
        call IntermissionRestore()
        set udg_Visibility = 72
        call IntermissionRestore()
        call GenericActions.execute()
        if udg_SquadXP>90000 then
            call ItemSpawnN('shas',6) //Ammunition Box
        else
            call ItemSpawnN('shas',8) //Ammunition Box
        endif
        call PolledWait(10.)
        call GeneralText(8.0, "|cfff3ad00Battalion|r : We have a large body of Zulus coming at you. Be advised." )
        call PolledWait(4.)
        set i = GetRandomInt(1,udg_Players)
        call FieldChat(udg_Hero[udg_Sorted[i]],5.0,udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : Hold Position!" )
        call PolledWait(10.)
        call Wave3Chatter.execute()
        call GankMarine(Player(13),'u003',2*udg_Spawn,2300,2800)
        call GankMarine(Player(14),'nzom',2*udg_Spawn,2300,2800)
        call GankMarine(Player(13),'u003',2*udg_Spawn,2300,2800)
        call PolledWait(10.)
        call GeneralText(8.0, "|cfff3ad00Battalion|r : Artillery support is on it's way. Hold your position! We don't want any friendly fucks." )
        call GankMarine(Player(13),'u003',2*udg_Spawn,2300,2600)
        call GankMarine(Player(14),'n004',2*udg_Spawn,2300,2600)
        call GroupEnumUnitsOfPlayer(g,Player(9),null)
        call FieldChat(udg_Ogilvy, 5.0, "|cfff3ad00Ogilvy|r : Come on, huddle up folks. I can't believe they're going to shell this area!" )
        call GankMarine(Player(13),'u003',2*udg_Spawn,2300,2600)
        call RemoveAllGuardPositions(Player(9))
        loop
            set u = FirstOfGroup(g)
            exitwhen u==null
            set x = -1095.0+GetRandomReal(-75.0,75.0)
            set y = -275.0+GetRandomReal(-100.0,100.0)
            call UnitRemoveAbility(u,'Aatk')
            call IssuePointOrder(u,"move",x,y)
            if GetRandomInt(0,10)==1 and democrats==0 then
                set democrats = 1
                call SimpleChat(u,5.0, "|cfff3ad00Civilian|r : I'm voting for the Democrats in the next election!" )
            endif
            call GroupRemoveUnit(g,u)
        endloop
        call DestroyGroup(g)
        set g = null
        set u = null
        call TriggerRegisterTimerEventPeriodic(t,6.0*udg_Pace)
        call TriggerAddAction(t,function ArtillerySupport)
        set t = null
        set i = GetRandomInt(1,udg_Players)
        call FieldChat(udg_Hero[udg_Sorted[i]], 5.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : Lure the rotters into arty fire!" )
        set i = 1
        loop
            exitwhen i>4
            loop
                exitwhen i2==1
                if GetRandomInt(0,1)==1 then
                    if GetRandomInt(0,1)==1 then
                        set udg_soundpath = "Sound\\Buildings\\Death\\BuildingDeathSmallHuman.wav"
                    else
                        set udg_soundpath = "Sound\\Buildings\\Death\\OrcBuildingDeathSmall1.wav"
                    endif
                    call PlaySoundPath.execute()
                endif
                call TriggerSleepAction(0.)
                if GetRandomInt(0,4)!=1 then
                    set i2=1
                endif
            endloop
            set i2=0
            call PolledWait(10.0*udg_Pace)
            call GankMarine(Player(14),'nzom',3*udg_Spawn,2500,3500)
            call GankMarine(Player(13),'n004',3*udg_Spawn,2500,3500)
            if GetRandomInt(0,1)==1 then
                if GetRandomInt(0,1)==1 then
                    set udg_soundpath = "Sound\\Buildings\\Death\\BuildingDeathSmallHuman.wav"
                else
                    set udg_soundpath = "Sound\\Buildings\\Death\\OrcBuildingDeathSmall1.wav"
                endif
                call PlaySoundPath.execute()
            endif
            call PolledWait(5.0*udg_Pace)
            if udg_SquadXP>30000 then
                call GankMarine(Player(13),'u000',2*udg_Spawn,2400,2900)
            else
                call GankMarine(Player(13),'u000',2+udg_Spawn,2400,2900)
            endif
            if GetRandomInt(0,2)==1 then
                call RandomSpawn('ugar',1,udg_Spawn) //Gargolyes
            elseif GetRandomInt(0,2)==1 then
                call RandomSpawn('nsw1',1,udg_Spawn) //Stalker
            endif
            set i=i+1
        endloop
        set udg_AITarget = null
        if udg_Players>0 then
            if GetRandomInt(1,3)==1 then
                call ExecuteRegisteredFunction("Wave4b")
               // call Wave4b.execute()
            else
                call ExecuteRegisteredFunction("Wave4a")
               // call Wave4a.execute()
            endif
        endif
    endfunction

    private function Wave3a takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 1
        call TriggerRegisterPlayerUnitEvent(t, Player(14), EVENT_PLAYER_UNIT_DEATH, null)
        call TriggerAddAction( t, function FemaleZombieEncounter )
        set t = null
        set udg_CurrentWave = 3
        set udg_Visibility = 75
        call IntermissionRestore()
        set udg_Visibility = 74
        call IntermissionRestore()
        call GenericActions.execute()
        call ItemSpawnN('shas',6) //Ammunition Box
        call FieldChat(udg_Ogilvy, 5.0, "|cfff3ad00Ogilvy|r : You've got to cover all the major entry points. Keep my people alive, Marines!" )
        call SpawnRectRandom(gg_rct_SpawnEastCave,Player(13),'nzom',udg_Spawn)
        if udg_Players<5 then
            call SpawnRectRandom(gg_rct_SpawnNorthWaterfall,Player(13),'nzom',udg_Spawn)
            call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(13),'nzom',udg_Spawn)
        else
            call SpawnRectRandom(gg_rct_SpawnNorthWaterfall,Player(13),'nzom',3+udg_Spawn)
            call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(13),'nzom',3+udg_Spawn)
        endif
        if GetRandomInt(0,1)==1 then
            call GeneralText(2.5, "|cfff3ad00Marine|r : Stay away from those corpses!" )
        else
            call GeneralText(2.5, "|cfff3ad00Marine|r : Don't let the children get out of sight. Zack's an equal opportunity killer." )
        endif
        call PolledWait(20.0*udg_Pace)
        call RandomSpawn('n00C',1,udg_Spawn) //Female Zombie
        call GankMarine(Player(13),'nzom',1+udg_Spawn,1800,2500)
        if udg_Players>=6 then
            call RandomSpawn('ugar',1,2)
            call RandomSpawn('n002',1,2)
        endif
        call PolledWait(30.0*udg_Pace)
        if udg_Mode==2 then
            call GeneralText(10.0, "|cfff3ad00Major Grimes|r : Read your ALICE manuals. Commands like -report, -team, and -tl provide useful real time statistics." )
        endif
        call RandomSpawn('n00N',1,GetRandomInt(0,2)) //Mass HP Zombie
        call SpawnRectRandom(gg_rct_SpawnTownWestCave,Player(13),'nzom',udg_Spawn)
        if udg_Mode==2 then
            call GeneralText(5.0, "|cfff3ad00Major Grimes|r : Make sure someone is covering you while you reload." )
        elseif udg_Players>5  and GetRandomInt(1,4)==1 then
            call GeneralText(2.5, "|cfff3ad00Marine|r : The Old Man wants a no BS situation report." )
            call PolledWait(3.)
            call GeneralText(3.0, "|cfff3ad00Marine 2|r : It ain't looking good with those NFGs in our squad." )
        endif
        call RandomSpawn('ugar',1,1+udg_Spawn)
        call RandomSpawn('n00C',1,1+udg_Spawn)
        call GankMarine(Player(13),'nzom',1+udg_Spawn,2300,3200)
        call PolledWait(30.0*udg_Pace)
        set i=1
        loop
            exitwhen i>udg_Players
            if udg_Experience[udg_Sorted[i]]<500 then
                call DisplayTimedTextToPlayer(ConvertedPlayer(udg_Sorted[i]),0.0,0.0,10.0,"Type -save to get your experience code once everyone is dead, and type -load in future games to load your accumulated experience. Experience grants access to more classes and abilities.")
            endif
            set i=i+1
        endloop
        set i=1
        call RandomSpawn('n00C',1,udg_Spawn) //Female Zombie
        set udg_soundpath = "Units\\Undead\\Banshee\\BansheeDeath.wav"
        call PlaySoundPath.execute()
        if udg_HitsTaken<10 then
            call GankMarine(Player(13),'nzom',2*udg_Spawn,2300,3200)
            call ItemSpawnN('I009',1) //RMS-12A Browning
        else
            call GankMarine(Player(13),'nzom',1+udg_Spawn,2300,3200)
        endif
        call PolledWait(30.0*udg_Pace)
        set udg_soundpath = "Units\\Undead\\Banshee\\BansheeDeath.wav"
        call PlaySoundPath.execute()
        if udg_Players>0 then
            if GetRandomInt(1,3)==1 then
                call ExecuteRegisteredFunction("Wave4b")
                //call Wave4b.execute()
            else
                call ExecuteRegisteredFunction("Wave4a")
                //call Wave4a.execute()
            endif
        endif
    endfunction


    private function init takes nothing returns nothing
        call RegisterFunction("Wave3a", function Wave3a)
        call RegisterFunction("Wave3b", function Wave3b)
        call RegisterFunction("Wave3c", function Wave3c)
    endfunction

endlibrary